package com.zxy.leetcode._00100_00199._00120_00129;

/**
 * https://leetcode-cn.com/problems/valid-palindrome/
 *
 * 125. 验证回文串
 * 给定一个字符串，验证它是否是回文串，只考虑字母和数字字符，可以忽略字母的大小写。
 * 说明：本题中，我们将空字符串定义为有效的回文串。
 */
public class Test00125 {

    public static void main(String[] args) {
        Test00125 test = new Test00125();
        System.out.println(test.isPalindrome(""));
        System.out.println(test.isPalindrome("A man, a plan, a canal: Panama"));
        System.out.println(!test.isPalindrome("race a car"));
    }

    public boolean isPalindrome(String s) {
        if (s == null || s.length() == 0) {
            return true;
        }

        char[] chars = s.toCharArray();
        int left = 0;
        int right = chars.length - 1;
        int letterX = Math.abs('a' - 'A');

        while (left < right) {
            char cl = chars[left];
            char cr = chars[right];
            if (Character.isDigit(cl)) {
                if (Character.isDigit(cr)) {
                    if (cl == cr) {
                        left ++;
                        right --;
                    }else {
                        return false;
                    }
                }else if (Character.isLetter(cr)) {
                    return false;
                }else {
                    right --;
                }
            }else if (Character.isLetter(cl)) {
                if (Character.isLetter(cr)) {
                    int x = Math.abs(cl-cr);
                    if (x == 0 || x == letterX) {
                        left ++;
                        right --;
                    }else {
                        return false;
                    }
                }else if (Character.isDigit(cr)) {
                    return false;
                }else {
                    right --;
                }
            }else {
                left ++;
            }

        }

        return true;
    }

}
